package com.zsz.controller;

import com.zsz.domain.Student;
import com.zsz.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Resource//@Autowired也行
    private StudentService service;

    //注册学生
    @RequestMapping("/addStudent.do")
    public ModelAndView addStudent(Student student){
        //model:数据    view：视图（页面）
        ModelAndView mv=new ModelAndView();

        String tips="注册失败";

        //调用service处理student
        int nums=service.addStudent(student);
        if (nums > 0){
            //注册成功
            tips="学生【"+student.getName()+"】注册成功";
        }
        //添加数据
        mv.addObject("tips",tips);

        //指定结果页面
        mv.setViewName("result");
        return mv;



    }

    //处理查询
    @RequestMapping("/queryStudent.do")
    @ResponseBody
    public List<Student> queryStudent(){
        //参数检查
        List<Student> students= service.findStudent();
        return students;

    }
}
